/*
 * 
 * Simple very simple client 
 * 
 */

package udpserver;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author makkalot
 */
public class UdpSimpleClient {

  public final static int DEFAULT_PORT = 9999;
  public final static String HOST_NAME= "localhost";
  public final static int MAX_IO_BUFFER = 4096;
  
  public static void main(String[] args) {

    String hostname;
    int port = DEFAULT_PORT; //port to connect here
    hostname=HOST_NAME;  // place to make a connection
        
    
    try {
      
      //the name of the server not so necessary  
      InetAddress server = InetAddress.getByName(hostname);
      
      //you will send data through that one
      DatagramSocket theSocket = new DatagramSocket();
      
      boolean stop_me = false;
      boolean first =true; // when should i send start mesage dimi ama
      byte[] binary_data = new byte[UdpSimpleClient.MAX_IO_BUFFER]; //allocate space for it
      
      //create the file hey it can throw IOException you know !
      LoadImage load_video = new LoadImage("send.avi"); //open the file  
 
      
      //to make other site to open its files and be ready for transmission
      String start_line  = "start";
      byte[] string_data = start_line.getBytes();
        
      //sending the hello packet
      DatagramPacket theOutput 
         = new DatagramPacket(string_data, string_data.length, server, port);
        //send the packet
      theSocket.send(theOutput);
      System.out.println("I sent the hello mesage");
      
      String theLine = "continue"; // continue your transmission
      while (!stop_me) {
        
        //the mesage to start transsmission
          
        if (theLine.equals("stop")){
            //if we are here we should send the last mesage to show to other part we are finished
            stop_me = true;
            string_data = theLine.getBytes();
            //you create a packet to send there
            DatagramPacket lastOutput 
            = new DatagramPacket(string_data, string_data.length, server, port);
            //send the packet
            theSocket.send(lastOutput);
            
            //close the file here
            load_video.finishReading();
            
            lastOutput = null; //nerden geldgin belli degil ya
            System.out.println("I sent the last mesage");
            continue; // to go out from the loop
        }
        
        //read a chunk from file and send it go go go !
        
        if(load_video.readThatByte(binary_data, UdpSimpleClient.MAX_IO_BUFFER)==-1){
            //if you come here that is the end of the game yoo
            theLine = "stop";
            continue;
        }
        
        //if you are here you can send you chunk ...
        //you create a packet to send there
        DatagramPacket binaryOutput 
         = new DatagramPacket(binary_data, binary_data.length, server, port);
        //send the packet
        theSocket.send(binaryOutput);
                try {
                    Thread.sleep(5);
                } catch (InterruptedException ex) {
                    Logger.getLogger(UdpSimpleClient.class.getName()).log(Level.SEVERE, null, ex);
                }
        
        }  // end while
    }  // end try
    catch (UnknownHostException e) {
      System.err.println(e);
    }  
    catch (SocketException e) {
      System.err.println(e);
    }
    catch (IOException e) {
      System.err.println(e);
    }

  }  // end main
    
}
